package com.songshuai.customer.client.controller;

import com.songshuai.customer.client.dto.request.frontsop.ArticleSopReqDto;
import com.songshuai.customer.client.dto.request.frontsop.CloseViewReqDto;
import com.songshuai.customer.client.dto.request.frontsop.GoodReqDto;
import com.songshuai.customer.client.dto.response.frontsop.ArticleSopResDto;
import com.songshuai.customer.client.service.FrontSopService;
import com.songshuai.customer.client.service.MyService;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;


@Slf4j
@Api("客户助手--我")
@RestController
@RequestMapping("/api/cust/my/")
public class MyController extends BaseController{

    @Autowired
    private MyService myService;

    @Autowired
    private FrontSopService frontSopService;



    @ApiOperation(value = "获取我的咨询顾问")
    @GetMapping(value = "mySale")
    public CompletableFuture<QueryResponse> mySale(){
        String currentUserId = this.getCurrentcustomerId();
        return CompletableFuture.supplyAsync(()-> {
            if(null == currentUserId){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                return myService.mySale(currentUserId);

            } catch (Exception e) {
                log.error("api/cust/my/mySale", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });

    }



    /**
     * sop点赞
     */
    @ApiOperation(value = "sop点赞")
    @RequestMapping(value = "goodSop", method = RequestMethod.POST)
    public CompletableFuture<QueryResponse> goodSop(@RequestBody GoodReqDto request){
        String currentUserId = this.getCurrentcustomerId();
        return CompletableFuture.supplyAsync(()-> {
            if(null == currentUserId){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                request.setCurrentUserId(currentUserId);
                frontSopService.goodSop(request);
                return QueryResponse.setResData(null);
            } catch (Exception e) {
                log.error("api/cust/my/goodSop", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });

    }


    /**
     * 关闭sop浏览
     */
    @ApiOperation(value = "关闭浏览")
    @RequestMapping(value = "closeView", method = RequestMethod.POST)
    public CompletableFuture<QueryResponse> closeView(@RequestBody CloseViewReqDto request){
        String currentUserId = this.getCurrentcustomerId();
        return CompletableFuture.supplyAsync(()-> {
            if(null == currentUserId){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                request.setCurrentUserId(currentUserId);
                frontSopService.closeView(request);
                return QueryResponse.setResData(null);
            } catch (Exception e) {
                log.error("api/cust/my/closeView", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });

    }



    /**
     * sop详情
     */
    @ApiOperation(value = "sop详情")
    @RequestMapping(value = "articleSop", method = RequestMethod.POST)
    public CompletableFuture<QueryResponse> articleSop(@RequestBody ArticleSopReqDto request){
        String currentUserId = this.getCurrentcustomerId();
        //String currentUserId = "10001";
        return CompletableFuture.supplyAsync(()-> {
            if(null == currentUserId){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                request.setCurrentUserId(currentUserId);
                ArticleSopResDto articleSopResDto = frontSopService.articleSop(request);
                return QueryResponse.setResData(articleSopResDto);
            } catch (Exception e) {
                log.error("api/cust/my/articleSop", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });
    }

    /**
     * 我参与过的活动
     */
    @ApiOperation("我参与过的活动数")
    @GetMapping("queryActivityCount")
    public CompletableFuture<QueryResponse> queryActivityCount(){
        String currentUserId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            if(StringUtils.isEmpty(currentUserId)){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                return myService.queryActivityCount(currentUserId);
            } catch (Exception e) {
                log.error("api/cust/my/queryActivityCount", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });
    }

    /**
     * 我参与过的活动
     */
    @ApiOperation("我参与过的活动")
    @GetMapping("queryActivityList")
    public CompletableFuture<QueryResponse> queryActivityList(){
        String currentUserId = this.getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            if(StringUtils.isEmpty(currentUserId)){
                return new QueryResponse(ResultStatus.FAIL.getErrorCode(),ResultStatus.FAIL.getErrorMsg());
            }
            try {
                return myService.queryActivityList(currentUserId);
            } catch (Exception e) {
                log.error("api/cust/my/queryActivityList", e);
                return new QueryResponse(ResultStatus.FAILOfficial.getErrorCode(),e.getMessage());
            }
        });
    }

}